﻿/*!
* jQuery ElGee Google Chart API Helper JavaScript Library v0.1.0.0.
* http://www.laurentgeffroy.com/
*
* Copyright 2011, Laurent Geffroy
* licensed under the GPL Version 2 license.
*/
(function ($) {
    $.fn.extend({
        elgeeChart: function (myMethod) {
            var methods = {
                getData: function ($obj) {

                    var data = $obj.data('elgeeChart');

                    if ((data.options.datasourceUrl != null) && (data.options.datasourceUrl.length > 0)) {
                        var jqxhr = $.getJSON(data.options.datasourceUrl, function (jsonData) {

                            data.options.datasource = jsonData.DataSource;
                            if (jsonData.ChartOptionsHaskKey != null) {
                                // Change the options only if the hashkey is different
                                if (data.options.hashKey != jsonData.ChartOptionsHaskKey) {
                                    data.options.hashKey = jsonData.ChartOptionsHaskKey;
                                    data.options.chartOptions = eval("(" + jsonData.JavascriptChartOptions + ")");
                                }
                            }
                        })
                            .success(function () { $obj.elgeeChart('render'); })
                            .error(function () { $obj.html("<div class='" + data.options.errorCssClass + "'>JSON Error while getting data at " + data.options.datasourceUrl + "</div>"); })
                            .complete(function () { });
                    }
                },

                render: function ($obj) {

                    var myArray = null;

                    function addColumn(type, name) {
                        datas.addColumn(type, name);
                    }

                    function renderAxis(axis) {
                        // axis 0 is Horizontal, 1 is Vertical
                        for (var i = 0; i < data.options.datasource.Tables.length; i++) {
                            if (data.options.datasource.Tables[i].AxisType == axis) {
                                var isString = true;
                                if (data.options.datasource.Tables[i].Type == 0)
                                    addColumn('string', data.options.datasource.Tables[i].Name);
                                else {
                                    isString = false;
                                    addColumn('number', data.options.datasource.Tables[i].Name);
                                }

                                // For each item of Data
                                for (var y = 0; y < data.options.datasource.Tables[i].Data.length; y++) {
                                    if ((myArray[y] == null) || (myArray[y] == 'undefined'))
                                        myArray[y] = new Array(0);

                                    if (isString)
                                        myArray[y].push(data.options.datasource.Tables[i].Data[y]);
                                    else
                                        myArray[y].push(parseFloat(data.options.datasource.Tables[i].Data[y].replace(",", ".")));
                                }
                            }
                        }
                    }

                    var data = $obj.data('elgeeChart');

                    var datas = new google.visualization.DataTable();

                    var cols;
                    if ((data.options.datasource != null) && (data.options.datasource != 'undefined') && (data.options.datasource.ItemsCount > 0)) {
                        // Define the size of the Array
                        myArray = new Array(data.options.datasource.ItemsCount);

                        // Build the columns For X
                        renderAxis(0);
                        // Build the columns For Y
                        renderAxis(1);
                        // Axis are now rendered, need to create the rows from myArray
                        if ((myArray != null) && (myArray.length == data.options.datasource.ItemsCount)) {
                            for (var i = 0; i < data.options.datasource.ItemsCount; i++) {
                                t = myArray[i];
                                datas.addRow(myArray[i]);
                            }
                        }
                    }



                    var ggv = null;
                    try {
                        switch (data.options.type) {
                            case "lineChart":
                                ggv = new google.visualization.LineChart(document.getElementById($obj.attr("id")));
                                break;
                            case "pieChart":
                                ggv = new google.visualization.PieChart(document.getElementById($obj.attr("id")));
                                break;
                            case "intensityMap":
                                ggv = new google.visualization.IntensityMap(document.getElementById($obj.attr("id")));
                                break;
                            case "areaChart":
                                ggv = new google.visualization.AreaChart(document.getElementById($obj.attr("id")));
                                break;
                            case "barChart":
                                ggv = new google.visualization.BarChart(document.getElementById($obj.attr("id")));
                                break;
                            case "columnChart":
                                ggv = new google.visualization.ColumnChart(document.getElementById($obj.attr("id")));
                                break;
                            case "gauge":
                                ggv = new google.visualization.Gauge(document.getElementById($obj.attr("id")));
                                break;
                            case "geoMap":
                                ggv = new google.visualization.GeoMap(document.getElementById($obj.attr("id")));
                                break;
                            case "imageAreaChart":
                                ggv = new google.visualization.ImageAreaChart(document.getElementById($obj.attr("id")));
                                break;
                        }
                        if (ggv != null)
                            ggv.draw(datas, data.options.chartOptions);

                        // new google.visualization.LineChart(document.getElementById($obj.attr("id"))).draw(datas, data.options.chartOptions);
                    }
                    catch (Error) {
                        $obj.html("<div class='" + data.options.errorCssClass + "'>" + Error.Message + "</div>");
                    }

                }
            };
            return this.each(function () {
                var $obj = $(this);
                if (typeof myMethod === "string" && methods[myMethod]) {
                    return methods[myMethod]($obj);
                } else if (typeof myMethod === "object" || !myMethod) {
                    var data = $obj.data('elgeeChart');
                    if (!data) {

                        $obj.data('elgeeChart', {
                            options: {
                                datasource: null,
                                chartOptions: null,
                                hashKey: null,
                                type: 'lineChart',
                                datasourceUrl: null,
                                automaticStart: true,
                                cssClass: null,
                                errorCssClass: null
                            }
                        });
                        data = $obj.data('elgeeChart');
                    }
                    if (myMethod) {
                        data.options = $.extend(data.options, myMethod);

                        if (data.options.automaticStart)
                            $obj.elgeeChart('render');
                        else {
                            if ((data.options.datasourceUrl != null) && (data.options.datasourceUrl.length > 0))
                                $obj.elgeeChart('getData');
                        }
                    }
                } else
                    $.error('Method ' + myMethod + ' does not exist on jQuery.elgeeChart');
            });
        }
    });
} (jQuery));